package com.sgcc.pda.lib_tools_database.comment;

import android.database.Cursor;
import android.util.Log;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

/**
 * 常用数据库操作方法封装
 */
public class CommonDaoUtils<T> {

    private AbstractDao<T, Long> mDao;

    public CommonDaoUtils(AbstractDao<T, Long> pEntityDao) {
        mDao = pEntityDao;
    }

    /**
     * 插入记录，如果表未创建，先创建表
     */
    public boolean insert(T pEntity) {
        try {
            if (null != mDao) {
                mDao.insertOrReplaceInTx(pEntity);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 插入多条数据
     */
    public boolean insert(List<T> list) {
        try {
            if (null != mDao) {
                mDao.insertOrReplaceInTx(list);

                Log.i("insertList", mDao.getClass().getSimpleName());
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 修改一条数据
     */
    public boolean update(T pEntity) {
        try {
            if (null != mDao) {
                mDao.updateInTx(pEntity);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 批量更新数据
     */
    public boolean update(List<T> list) {
        try {
            if (null != mDao) {
                mDao.updateInTx(list);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除单条记录
     */
    public boolean delete(T pEntity) {
        try {
            if (null != mDao) {
                mDao.delete(pEntity);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除多条记录
     */
    public boolean delete(List<T> list) {
        try {
            if (null != mDao) {
                mDao.deleteInTx(list);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除所有记录
     */
    public boolean deleteAll() {
        try {
            if (null != mDao) {
                mDao.deleteAll();
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查询所有记录
     */
    public List<T> queryAll() {
        try {
            if (null != mDao) {
                return mDao.loadAll();
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据主键id查询记录
     */
    public T queryById(long key) {
        try {
            if (null != mDao) {
                return mDao.load(key);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 使用native sql进行查询操作
     */
    public List<T> query(String sql, String[] conditions) {
        try {
            if (null != mDao) {
                return mDao.queryRaw(sql, conditions);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 使用native sql进行查询操作
     */
    public Cursor queryCustom(String sql, String[] conditions) {
        try {
            return mDao.getDatabase().rawQuery(sql, conditions);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用queryBuilder进行查询
     */
    public List<T> query(WhereCondition cond, WhereCondition... condMore) {
        try {
            if (null != mDao) {
                QueryBuilder<T> queryBuilder = mDao.queryBuilder();
                return queryBuilder.where(cond, condMore).list();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<T> query(Property[] wheres, String[] params) {
        List<T> list = new ArrayList<>();
        try {
            if (null != mDao) {
                QueryBuilder queryBuilder = mDao.queryBuilder();
                for (int i = 0; i < wheres.length; i++) {
                    queryBuilder.where(wheres[i].eq(params[i]));
                }
                list.addAll(queryBuilder.list());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public T query(Property wheres, String params) {
        T object = null;
        try {
            if (null != mDao) {
                object = mDao.queryBuilder().where(wheres.eq(params)).unique();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    public T querys(Property[] wheres, String[] params) {
        T object = null;
        try {
            if (null != mDao) {
                QueryBuilder queryBuilder = mDao.queryBuilder();
                for (int i = 0; i < wheres.length; i++) {
                    queryBuilder.where(wheres[i].eq(params[i]));
                }
                object = (T) queryBuilder.unique();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    /**
     * 查询排序
     *
     * @param wheres
     * @param params
     * @param properties 需要排序的字段
     * @param isAsc      是否按asc排序
     * @return
     */
    public List<T> query(Property[] wheres, String[] params,
                         Property properties, boolean isAsc) {
        List<T> objects = null;
        try {
            if (null == mDao) {
                return null;
            }
            QueryBuilder queryBuilder = mDao.queryBuilder();
            for (int i = 0; i < wheres.length; i++) {
                queryBuilder.where(wheres[i].eq(params[i]));
            }
            if (isAsc) {
                queryBuilder.orderAsc(properties);
            } else {
                queryBuilder.orderDesc(properties);
            }
            objects = queryBuilder.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objects;
    }

    public long queryCount(Property wheres, String params) {
        long count = 0;
        try {
            if (null != mDao) {
                count = mDao.queryBuilder().where(wheres.eq(params)).count();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    public long queryCount() {
        long count = 0;
        try {
            if (null != mDao) {
                count = mDao.queryBuilder().count();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    public long queryCount(Property[] wheres, String[] params) {
        long count = 0;
        try {
            if (null == mDao) {
                return 0;
            }
            QueryBuilder queryBuilder = mDao.queryBuilder();
            for (int i = 0; i < wheres.length; i++) {
                queryBuilder.where(wheres[i].eq(params[i]));
            }
            count = queryBuilder.count();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

}
